
const express = require('express')
const md5 = require('md5')

// 引入正则校验中间件
const addAdminReg = require('../middleware/addAdminReg')

// 引入adminModel集合
const adminModel = require('../db/model/admin')

const route = express.Router()

// 添加管理员接口(添加正则校验中间件)
route.post('/addAdmin',addAdminReg,async (request, response) => {
    const { adminName, password, regTime } = request.body
    console.log(adminName, password, regTime);


    try {
        const findresult = await adminModel.findOne({  //adminModel.findOne返回一个promise实例 
            adminName,
        })
        if (findresult) {
            return response.send({
                code: 10001,
                msg: '用户信息已经存在',
                data: 'null'
            })
        }
        await adminModel.create({
            adminName,
            password:md5(password+'34345'),
            regTime
        })
        response.send({
            code: 10000,
            msg: '信息添加成功',
            data: null
        })
    } catch (e) {
        console.log('服务器错误异常' + e.message);
        response.send({
            code: 10002,
            msg: '服务器处理异常',
            data: null
        })
    }


})

module.exports = route;